package com.sinotrust.classify.algorithm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.kernelab.basis.Tools;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

import svm.svm_predict;
import svm.svm_train;

public class Svm {
	public String parametertrain;
	public String parameterpredict;
	public String inputdatafile;
	public String model;
	public String testFile;
	public String outputdatefile;
	
	public void train(String datasetfile) throws IOException{
		StringBuffer buffer = new StringBuffer();
		buffer.append("-v 10").append(" ").append(datasetfile);
		String[] targvs = buffer.toString().split(" ");
		svm_train t = new svm_train();
		t.run(targvs);
	}

	public void train() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(parametertrain).append(" ");
		buffer.append(inputdatafile).append(" ");
		buffer.append(model).append(" ");
		String[] targvs = buffer.toString().split(" ");
		svm_train t = new svm_train();
		try {
			t.run(targvs);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void predict() {
		StringBuffer buffer = new StringBuffer();
		svm_predict p = new svm_predict();
		buffer.append(parameterpredict).append(" ");
		buffer.append(testFile).append(" ");
		buffer.append(model).append(" ");
		buffer.append(testFile+"_result").append(" ");
		String[] pargvs = buffer.toString().split(" ");
		try {
			p.predict(pargvs);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}

	public void analyser(String string,Double thredhold) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(string));
		String tmp;
		String[] tmp1;
		int pn = 0;
		int nn = 0;
		int pnp = 0;
		int nnp = 0;
		int terminal;
		// Tools.debug(tmp = br.readLine());
		while ((tmp = br.readLine()) != null) {
			tmp1 = tmp.split(" ");
			if (tmp1[0].equals("-1"))
				nn++;
			if (tmp1[0].equals("1"))
				pn++;
		}
		Tools.debug("非商业网站的数量是：" + nn);
		Tools.debug("商业网站的数量是：" + pn);
		terminal = nn;
		int i = 0;
		BufferedReader br1 = new BufferedReader(new FileReader(string+"_result"));
		while ((tmp = br1.readLine()) != null) {
			// Tools.debug(tmp);
			tmp1 = tmp.split(" ");
			if (0 < i && i <= terminal) {
				// Tools.debug(Double.parseDouble(tmp1[0])+1);
				if (Math.abs(Double.parseDouble(tmp1[1])) > thredhold)
					nnp++;
			}
			if (i > terminal) {
				// Tools.debug(Double.parseDouble(tmp1[0])-1);
				if (Math.abs(Double.parseDouble(tmp1[1])) < thredhold)
					pnp++;
			}
			i++;
		}

		Tools.debug("对非商业网站的预测准确率是：" + (double) nnp / nn);
		Tools.debug("对商业网站的预测准确率是：" + (double) pnp / pn);
		br1.close();
		br.close();
	}

	public void resultOutput(String output) throws NumberFormatException,
			IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(output));
		String tmp;
		String[] tmp1;
		int i = 0;
		// Tools.debug(tmp = br.readLine());
		while ((tmp = br.readLine()) != null) {
			if (i > 0) {
				tmp1 = tmp.split(" ");
				// Tools.debug(tmp1[1]);
				if (Double.parseDouble(tmp1[1]) > 0.5) {
					Tools.debug("id" + "\t" + i + "\t" + "不是一个商业网站");
				}
				if (Double.parseDouble(tmp1[1]) < 0.5) {
					Tools.debug("id" + "\t" + i + "\t" + "是一个商业网站");
				}
			}
			i++;
		}
		br.close();
	}

	public void errorDataOutput(String orgnalData, String pastData)
			throws IOException {
		// TODO Auto-generated method stub
		BufferedReader brPast = new BufferedReader(new FileReader(pastData));
		BufferedReader brOrignal = new BufferedReader(new FileReader( orgnalData));
		BufferedWriter newClassify = new BufferedWriter(new FileWriter( "newdataclassify.txt"));
		String tmp;
		String[] tmp1;
		int number = count(pastData) - 1;
		int counter = -1;
		String[] pd = new String[number];
		double[] past = new double[number];
		double[] orignal = new double[number];
		while ((tmp = brPast.readLine()) != null) {
			if (counter > -1) {
				tmp1 = tmp.split(" ");
				past[counter] = Double.parseDouble(tmp1[0]);
//				Tools.debug(pd[counter]);
			}
			counter++;
		}
//		Tools.debug("=====================");
		counter = 0;
		while ((tmp = brOrignal.readLine()) != null) {
			tmp1 = tmp.split(" ");
			orignal[counter] = Double.parseDouble(tmp1[0]);
			pd[counter] = tmp;
			counter++;
		}
		counter = 0;
		for (int i = 0; i < number; i++) {
			if ((Math.abs(orignal[i] - past[i]) < 0.0001)
					&& (Math.abs(past[i] - 1) < 0.0001)||( (Math.abs(past[i] + 1)<0.0001))) {
				Tools.debug(pd[i]);
				newClassify.write(pd[i]+"\n");
			}
		}
	}

	private int count(String pastData) throws IOException {
		// TODO Auto-generated method stub
		int number = 0;
		BufferedReader brPast = new BufferedReader(new FileReader(pastData));
		while (brPast.readLine() != null) {
			number++;
		}
		return number;
	}
}
